/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import domain.Bankaccount;
import domain.Contract;
import domain.Electricityconsumption;
import domain.Electricitycontract;
import domain.Expense;
import domain.Income;
import domain.Insurancecontract;
import domain.Invoice;
import domain.Mobilecontract;
import domain.Mortgagecontract;
import domain.Note;
import domain.Task;
import domain.Userdom;
import domain.Watercontract;
import domain.Wcontract;
import domain.Vehiclecar;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import planner.Operations;

/**
 *
 * @author Alberto
 */
public class Delete extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private String type="";
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            type = request.getParameter("type");
            Userdom u = (Userdom) request.getSession().getAttribute("user");

            if (type.equals("note")) {
                String idNote = request.getParameter("idNote");
                Operations o = new Operations();
                Note n = o.getNote(Integer.parseInt(idNote));
                o.removeNote(n);
                Set s = new HashSet(u.getNotes());
                s.remove(n);
                u.setNotes(s);
            } else if (type.equals("task")) {
                String idTask = request.getParameter("idTask");
                Operations o = new Operations();
                Task task = o.getTask(Integer.parseInt(idTask));
                o.removeTask(task);
                Set s = new HashSet(u.getTasks());
                s.remove(task);
                u.setTasks(s);
            } else if (type.equals("income")){
                Integer idIncome = Integer.parseInt(request.getParameter("idIncome"));
                Operations o = new Operations();
                Income income = o.getIncome(idIncome);
                o.removeIncome(income);
                income.getBankaccount().getIncomes().remove(income);
                u.getBankaccount(income.getBankaccount().getIdBank()).setIncomes(income.getBankaccount().getIncomes());
            }  else if (type.equals("expense")){
                Integer idExpense = Integer.parseInt(request.getParameter("idExpense"));
                Operations o = new Operations();
                Expense expense = o.getExpense(idExpense);
                o.removeExpense(expense);
                expense.getBankaccount().getExpenses().remove(expense);
                u.getBankaccount(expense.getBankaccount().getIdBank()).setExpenses(expense.getBankaccount().getExpenses());

            }else if (type.equals("electricityContract")){
                Integer idElectricityContract = Integer.parseInt(request.getParameter("idElectricityContract"));
                Operations o = new Operations();
                Electricitycontract eCD = o.getElectricityContract(idElectricityContract);
                Contract c = eCD.getContract();

                o.removeElectricityContract(eCD);
                o.removeContract(c);
                
                List listE = o.getElectricityInvoiceList(eCD);
                Iterator it1 = listE.iterator();
                while (it1.hasNext()){
                    Invoice invoice = (Invoice) it1.next();
                    Iterator it2 = invoice.getElectricityconsumptions().iterator();
                    while(it2.hasNext()){
                        Electricityconsumption eCons = (Electricityconsumption) it2.next();
                        o.removeElectricityConsumption(eCons);
                        o.removeConsumption(eCons.getConsumption());
                    }
                    o.removeInvoice(invoice);
                }


            } else if (type.equals("mortgageContract")){
                Integer idMortgageContract = Integer.parseInt(request.getParameter("idMortgageContract"));
                Operations o = new Operations();
                Mortgagecontract mCD = o.getMortgageContract(idMortgageContract);
                Contract cm = mCD.getContract();

                o.removeMortgageContract(mCD);
                o.removeContract(cm);
            } else if(type.equals("insuranceContract")){
                Integer idInsuranceContract = Integer.parseInt(request.getParameter("idInsuranceContract"));
                Operations o = new Operations();
                Insurancecontract iCD = o.getInsuranceContract(idInsuranceContract);
                Contract ci = iCD.getContract();

                o.removeInsuranceContract(iCD);
                o.removeContract(ci);
            } else if(type.equals("waterContract")){
                Integer idWaterContract = Integer.parseInt(request.getParameter("idWaterContract"));
                Operations o = new Operations();
                Watercontract wCD = o.getWaterContract(idWaterContract);
                Contract wc = wCD.getContract();

                o.removeWaterContract(wCD);
                o.removeContract(wc);
            } else if (type.equals("mobileContract")){
                Integer idMobileContract = Integer.parseInt(request.getParameter("idMobileContract"));
                Operations o = new Operations();
                Mobilecontract mCD = o.getMobileContract(idMobileContract);
                Contract mc = mCD.getContract();

                o.removeMobileContract(mCD);
                o.removeContract(mc);
            } else if (type.equals("bankAccount")){
                Integer idMBankAccount = Integer.parseInt(request.getParameter("idBankAccount"));
                Operations o = new Operations();
                Bankaccount b = o.getBankAccount(idMBankAccount);
                
                o.removeBankaccount(b);
            }else if (type.equals("wCont")) {
                String id = request.getParameter("idWContract");
                Integer idW = Integer.parseInt(id);
                Operations o = new Operations();

                    Wcontract wc =  o.getWcontract(idW);

                    o.removeWcontract(wc);
            }else if (type.equals("vehiclecar")) {
                String id = request.getParameter("idVehiclecar");
                Integer idW = Integer.parseInt(id);
                Operations o = new Operations();
                Vehiclecar v = o.getVehiclecar(idW);

                o.removeVehiclecar(v);
            }

        } finally {
            String url = request.getParameter("redir");

            RequestDispatcher d = request.getRequestDispatcher("./FrontController?redir="+url);
                d.forward(request, response);
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
